//
//  DownloadManager.m
//  Leisure
//
//  Created by 大展 on 16/4/25.
//  Copyright © 2016年 张展. All rights reserved.
//

#import "DownloadManager.h"

@interface DownloadManager ()<DownloadDelegate>

@property (nonatomic, strong)NSMutableDictionary *dic; // 用来记录当前正在下载的对象;

@end

@implementation DownloadManager

- (NSMutableArray *)downloadAray {

    if (!_downloadAray) {

        _downloadAray = [NSMutableArray array];
    }
    return _downloadAray;
}

- (NSMutableDictionary *)dic {

    if (!_dic) {

        _dic = [NSMutableDictionary dictionary];
    }
    return _dic;
}

+ (DownloadManager *)sharedManager {

    static DownloadManager *handle = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        handle = [[DownloadManager alloc]init];
    });
    return handle;
}

// 根据URL添加一个下载
- (Download *)addDownloadWithModel:(RadioDetailModel *)model {

    // 判断当前的下载的对象有没有正在下载
    Download *download = self.dic[model.musicUrl];
    if (!download) {

        // 创建一个下载对象
        download = [[Download alloc]initWithUrl:model.musicUrl];
        download.title = model.title;
        download.delegate = self;
        // 开始下载
        [download startDownload];

        [self.dic setValue:download forKey:model.musicUrl];

        [self.downloadAray addObject:download];
    }
    return download;
}

// 根据url找到一个download
- (Download *)findDownloadWithModel:(RadioDetailModel *)model {

    return self.dic[model.musicUrl];
}

#pragma mark DownloadDelegate 协议方法

- (void)didFinishDownloadWithURL:(NSString *)url {

    [self.dic removeObjectForKey:url];
    NSLog(@"已经移除");
}









@end
